package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Endereco;
import br.com.fafica.sisape.model.Paciente;
import br.com.fafica.sisape.model.Pessoa;

public class DAOUsuario{

	public void createPaciente(Paciente paciente) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Usuario "
				+ "(Nome, Cpf, Rua, Cidade, Sexo, Telefone, Login, Senha) "
				+ "values (?,?,?,?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, paciente.getNome());
			stmp.setString(2, paciente.getCpf());
			stmp.setString(3, paciente.getEndereco().getRua());
			stmp.setString(4, paciente.getEndereco().getCidade());
			stmp.setString(5, paciente.getSexo());
			stmp.setString(6, paciente.getTelefone());
			stmp.setString(7, paciente.getLogin());
			stmp.setString(8, paciente.getSenha());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	public ArrayList<Pessoa> readPaciente() throws SQLException {
		Connection con = new ConnectionFactory().getConnection();

		String sql = "Select * from Usuario";

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Pessoa> lista = new ArrayList<Pessoa>();
			while (rs.next()) {
				Paciente paciente = new Paciente();
				Endereco endereco = new Endereco();
				paciente.setCod(rs.getInt("Cod"));
				paciente.setNome(rs.getString("Nome"));
				paciente.setCpf(rs.getString("Cpf"));
				endereco.setRua(rs.getString("Rua"));
				endereco.setCidade(rs.getString("Cidade"));
				paciente.setEndereco(endereco);
				paciente.setSexo(rs.getString("Sexo"));
				paciente.setTelefone(rs.getString("Telefone"));
				paciente.setLogin(rs.getString("Login"));
				paciente.setSenha(rs.getString("Senha"));
				paciente.setCod(rs.getInt("Cod"));
				lista.add(paciente);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	public void updatePaciente(Paciente paciente) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Usuario set Nome=?"+" , Cpf=?"
				+" ,Rua=?"
				+ " ,Cidade=?" + " ,Sexo=?"
				+ " ,Telefone=?"
				+ " ,Login=?" + ", Senha=?"
				+ "where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, paciente.getNome());
			stmp.setString(2, paciente.getCpf());
			stmp.setString(3, paciente.getEndereco().getRua());
			stmp.setString(4, paciente.getEndereco().getCidade());
			stmp.setString(5, paciente.getSexo());
			stmp.setString(6, paciente.getTelefone());
			stmp.setString(7, paciente.getLogin());
			stmp.setString(8, paciente.getSenha());
			stmp.setInt(9, paciente.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	public void deletePaciente(Paciente p) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Usuario where Cod=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, p.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}
}
